package com.liuyoung.clockview;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.BatteryManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.util.LogPrinter;
import android.view.View;
import android.widget.Button;
import android.widget.TextClock;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    BatteryView mBatteryView ;
    Button mButton ;
    TextView textview;
    WifiView mWifiView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mBatteryView = (BatteryView) findViewById(R.id.batteryview);

        mWifiView= (WifiView) findViewById(R.id.wifiview);

        mButton= (Button) findViewById(R.id.button);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Log.d("abc","ischarge "+mBatteryView.getIsCharge());
//                mBatteryView.setIsCharge(!mBatteryView.getIsCharge());
                startActivity(new Intent(getApplicationContext(),Main2Activity.class));
            }
        });

        textview= (TextView) findViewById(R.id.textview);
        textview.setMovementMethod(ScrollingMovementMethod.getInstance());
    }

    @Override
    protected void onStart() {
        super.onStart();
        this.registerReceiver(mBatteryReceiver,new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
        IntentFilter filter = new IntentFilter();
        filter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
        filter.addAction(WifiManager.RSSI_CHANGED_ACTION);
        this.registerReceiver(mWifiReceiver,filter);
    }

    @Override
    protected void onPause() {
        super.onPause();
        this.unregisterReceiver(mBatteryReceiver);
        this.unregisterReceiver(mWifiReceiver);
    }

    //电池广播
    private BroadcastReceiver mBatteryReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
            int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 0);
            int levelPercent = (int) (((float) level / scale) * 100);//电量

            int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, BatteryManager.BATTERY_STATUS_UNKNOWN);//充电状态
            String strStatus = "未知状态";

            if (status == BatteryManager.BATTERY_STATUS_CHARGING){
                strStatus = "充电中……";
                Log.d("abc",strStatus);
                mBatteryView.setIsCharge(true);
            }else{
                textview.setText(textview.getText()+"\n"+levelPercent);
                mBatteryView.setPower(levelPercent);
                mBatteryView.setIsCharge(false);
            }

        }
    };

    //无线监听
    private BroadcastReceiver mWifiReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
                int wifistate = intent.getIntExtra(
                        WifiManager.EXTRA_WIFI_STATE,
                        WifiManager.WIFI_STATE_DISABLED);

                if (wifistate == WifiManager.WIFI_STATE_DISABLED) {
                    mWifiView.setIsWifiDisabled(false);
                } else if (wifistate == WifiManager.WIFI_STATE_ENABLED) {
                    changeWifiStrenght();
                }
            } else if (action.equals(WifiManager.RSSI_CHANGED_ACTION)) {
                initWifiState();
            }
        }
    };

    public void initWifiState() {

        ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = manager
                .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (info.isConnected()) {
            changeWifiStrenght();
        } else {
            mWifiView.setIsWifiDisabled(false);
        }
    }

    private void changeWifiStrenght(){
        mWifiView.setIsWifiDisabled(true);
        mWifiView.setPower(getStrength(this));
    }

    public int getStrength(Context context) {
        WifiManager wifiManager = (WifiManager) context
                .getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = wifiManager.getConnectionInfo();
        if (info.getBSSID() != null) {
            int strength = WifiManager.calculateSignalLevel(info.getRssi(), 4);
            return strength;
        }
        return 0;
    }

}
